//***************************************//
//**       mMath 02-01-2012            **//
//**    Riku Maikola rpm@ac-mapa.org   **//
// **************************************//
#include"mMath.h"
//Math functions
double tb_CircleArea(double R)
{
    return PI*(R*R);
}

//Etäisyys
double Distance2D(double x1,double y1,double x2,double y2)
{
    double x = double(x1-x2);
    double y = double(y1-y2);
    double d = sqrt((x*x)+(y*y));

    return d;
};

//**********************//
//**    Etäisyys      **//
//**********************//
double Distance3D(double x1,double y1,double z1,double x2,double y2,double z2)
{
    double x = x1-x2;
    double y = y1-y2;
    double z = z1-z2;
    return sqrt((x*x)+(y*y)+(z*z));
};

//
double cd_rpm(double Vc,double Dc)
{
    return (Vc*1000)/(PI*Dc);
}
double cd_CuttingSpeed(double n,double Dc)
{
    return (n*PI*Dc)/1000;
}
double cd_rpmForCopying(double Vc,double Dc,double Ap)
{
    return (Vc*1000)/(PI*(2*(sqrt(Ap*(Dc-Ap)))));
}
double cd_FeedSpeed(double n,double Zc,double Fz)
{
    return n*Zc*Fz;
}
double cd_FeedPerRevolution(double Zc,double Fz)
{
    return Zc*Fz;
}
double cd_InternalCircularInterpolation(double Do,double Di,double Dc)
{
    return (((Do*Do)-(Di*Di)) / (4*(Do-Dc)));
}
double cd_ExternalCircularInterpolation(double Do,double Di,double Dc)
{
    return (((Do*Do)-(Di*Di)) / (4*(Di+Dc)));
}
double cd_FeedSpeedInterpolationInternal(double D,double Dc,double n,double Zc,double Fz)
{
    return ((D-Dc)*n*Zc*Fz)/D;
}
double cd_FeedSpeedInterpolationExternal(double D,double Dc,double n,double Zc,double Fz)
{
    return ((D+Dc)*n*Zc*Fz)/D;
}

//***********************************************************************************************************//
//**
//***********************************************************************************************************//

//MATCH.....
bool AER(double A, double B, double maxRelativeError)
{
    if (A == B)
        return true;
    double relativeError;
    if (fabs(B) > fabs(A))
        relativeError = fabs((A - B) / B);
    else
        relativeError = fabs((A - B) / A);
    if (relativeError <= maxRelativeError)
        return true;
    return false;
};
//MATCH.....
bool IsInArc(double A, double B,double C)
{
    //if(C>PI2)C=C-PI2;
    //if(C<0)C=C+PI2;
    if(B>A && C>A && B>C)return true;
    if(A>B)
    {
        if(C>A)return true;
        if(C<B)return true;
    }
    if(AER(A,C,0.0001))return true;
    if(AER(B,C,0.0001))return true;
    return false;
};
